//
//  CircleView.m
//  test
//
//  Created by Li Shen on 2021/4/12.
//  Copyright © 2021 lyh. All rights reserved.
//

#import "IKCircleView.h"
#import "IKCircularProgressView.h"

#define PROGRESS_WIDTH  KScreenW <= 320 ? 160 : scale(140)
#define TOAST_DISPLAY_TIME 5

@interface IKCircleView ()


@property (nonatomic, strong) UILabel *heartlabel;
@property (nonatomic, strong) UILabel *bmpLable;

@property (nonatomic, strong) IKCircularProgressView * circularProgressView;
@property (nonatomic, strong) IKCircularProgress *circularProgress;
@property (nonatomic, strong) UILabel *toastLabel;

@property (nonatomic, assign) NSInteger progress;


@end
@implementation IKCircleView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    self.backgroundColor = ECG_COLOR_GREEN;
    self.heartlabel = [[UILabel alloc] init];
    self.heartlabel.font = Font(60);
    self.heartlabel.textColor = ECG_COLOR_WHITE;
    self.heartlabel.text = @"00";
    self.heartlabel.textAlignment = NSTextAlignmentCenter;
    
    self.bmpLable = [[UILabel alloc] init];
    self.bmpLable.font = Font(20);
    self.bmpLable.textColor = ECG_COLOR_WHITE;
    self.bmpLable.text = @"BPM";
    self.bmpLable.textAlignment = NSTextAlignmentCenter;
    
    self.toastLabel = [[UILabel alloc] init];
    self.toastLabel.font = KScreenW <= 320 ? [UIFont systemFontOfSize:15] : [UIFont systemFontOfSize:17];
    self.toastLabel.textColor = [UIColor whiteColor];
    self.toastLabel.text = INTERNATIONAL(@"string_press_power_btn");
    
    self.progress = 0;
    [self initCirclePogressView];
    
    [self addSubview:self.circularProgressView];
    [self addSubview:self.heartlabel];
    [self addSubview:self.bmpLable];
    [self addSubview:self.toastLabel];
}

- (void)initCirclePogressView{
    self.circularProgress = [[IKCircularProgress alloc] init];
    self.circularProgressView = [IKCircularProgressView initWithFrame:CGRectMake(0, 0, scale(PROGRESS_WIDTH), scale(PROGRESS_WIDTH)) circularProgress:self.circularProgress];
    
    self.circularProgress.progressColor = [UIColor whiteColor];
    self.circularProgress.progressBGColor = ECG_RGBCOLOR(0x25, 0xb8, 0x68);
    self.circularProgress.startTextColor = [UIColor whiteColor];
    self.circularProgress.endTextColor = [UIColor whiteColor];
    self.circularProgress.progressWidth = 10;
    self.circularProgress.progressBGWidth = 8;
    self.circularProgress.startText = @"0";
    self.circularProgress.endText = @"40";
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    [self.circularProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.size.equalTo(@(CGSizeMake(scale(PROGRESS_WIDTH), scale(PROGRESS_WIDTH))));
        make.top.equalTo(self.mas_top).offset(50);
    }];
    
    [self.heartlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.circularProgressView);
        make.height.equalTo(@(70));
        make.width.equalTo(@(140));
    }];

    [self.bmpLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.heartlabel.mas_bottom).offset(20);
    }];
    
    int offset = (KScreenH <= 1136) ? 10 : 30;
    [self.toastLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.circularProgressView.mas_bottom).offset(offset);
        make.centerX.equalTo(self);
    }];
}

-(void)setToastState:(NSString*)toast;{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.toastLabel.text = toast;
        [self.toastLabel.layer addAnimation:[self showAnimationDuration:2] forKey:nil];
    });
}

- (void)setProgress:(NSInteger)progress{
    _progress = progress;
    self.circularProgressView.progress = progress;
}

- (void)setDuration:(NSInteger)duration{
    self.circularProgress.endText = [NSString stringWithFormat:@"%ld",(long)duration];
}

- (void)setHeartValue:(NSString*)heart{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.heartlabel.text = heart;
    });
}

- (void)resetUI{
    [self.circularProgressView clearProgress];
    self.heartlabel.text = @"00";
}

- (CATransition *)showAnimationDuration:(CGFloat)duration
{
    CATransition *animation = [CATransition animation];
    animation.duration = duration;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    animation.fillMode = kCAFillModeForwards;
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromRight;
    return animation;
}


@end
